<template>
  <div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('现金流类型')" prop="type">
        <el-select v-model="queryParams.type" :placeholder="$t('现金流类型')" clearable>
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('发生时间')" prop="operateDate">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />
      </el-form-item>
      <el-form-item :label="$t('方向')" prop="direction">
        <el-select v-model="queryParams.direction" :placeholder="$t('方向')" clearable>
          <el-option
            v-for="dict in directionOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('业务类型')" prop="businessType">
        <el-select v-model="queryParams.businessType" :placeholder="$t('业务类型')" clearable>
          <el-option
            v-for="dict in businessTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['lpbase:cashflow:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['lpbase:cashflow:view']"-->
      <!--          type="primary"-->
      <!--          icon="el-icon-info"-->
      <!--          size="mini"-->
      <!--          @click="getIrr"-->
      <!--        >{{ $t("获取IRR") }}</el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="lpCashflowList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('现金流类型')" align="center" prop="type" :formatter="typeFormat" show-overflow-tooltip />
      <el-table-column :label="$t('业务类型')" align="center" prop="businessType" :formatter="businessTypeFormat" show-overflow-tooltip />
      <el-table-column :label="$t('发生时间')" align="center" prop="operateDate" show-overflow-tooltip width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operateDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('发生金额') + $t('(元)')" align="center" prop="operateAmount" show-overflow-tooltip width="100">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.direction=== 'LC'? 0- scope.row.operateAmount: scope.row.operateAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('方向')"
        align="center"
        prop="direction"
        :formatter="directionFormat"
        show-overflow-tooltip
        width="90"
      />
      <el-table-column
        :label="$t('是否参与IRR计算')"
        align="center"
        prop="whetherIrr"
        :formatter="whetherIrrFormat"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['lpbase:cashflow:edit']"
            size="small"
            type="text"
            :icon=" 'el-icon-edit'"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出资人现金流对话框 -->
    <el-dialog :title="title" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <LpCashflowForm ref="LpCashflowForm" />
    </el-dialog>
  </div>
</template>

<script>
  import { listLpCashflow, delLpCashflow, getLpCashflowIrr } from '@/api/fund/lpBase/cashflow/lpCashflow'
  import LpCashflowForm from '../cashflow/lpCashflowForm'
  import { getFundBase } from '@/api/fund/fundBase'
  import { getSystemCurrency } from '@/api/system/config'
  import { getCashFlowCombined } from '@/api/fund/lpBase/cashflow/lpCashflow'
  export default {
    components: {
      LpCashflowForm
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 出资人现金流表格数据
        lpCashflowList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        formOpen: false,
        isRate: false,
        fundCurr: '',
        defCurr: '',
        // 发生时间范围
        dateRange: [],
        // 现金流类型  预计 实际字典
        typeOptions: [],
        // 方向：流入、流出字典
        directionOptions: [],
        // 业务类型：基金缴款 基金分配字典
        businessTypeOptions: [],
        // 是否参与IRR计算字典
        whetherIrrOptions: [],
        // 查询参数
        queryParams: {
          type: undefined,
          lpName: undefined,
          fundId: undefined,
          lpId: this.$route.query.id,
          operateDate: undefined,
          direction: undefined,
          businessType: undefined,
          whetherIrr: undefined,
          pageNum: 1,
          pageSize: 10
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        cashflowVO: {},
        cashflowQuarterOrMonth: false
      }
    },
    created() {
      this.initCurr()
      // 查询基金现金流合计信息
      this.getCashFlowCombined()
      this.getList()
      // 现金流类型
      this.getDicts('cash_flow_type').then(response => {
        this.typeOptions = response.data
      })
      this.getDicts('cash_flow_direction').then(response => {
        this.directionOptions = response.data
      })
      this.getDicts('lpbase_cashflow_business_type').then(response => {
        this.businessTypeOptions = response.data
      })
      this.getDicts('sys_yes_no').then(response => {
        this.whetherIrrOptions = response.data
      })
    },
    methods: {
      init(id) {
        this.queryParams.fundId = id
        this.getList()
      },
      /** 查询出资人现金流列表 */
      getList() {
        this.loading = true
        listLpCashflow(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.lpCashflowList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 现金流类型  预计 实际字典翻译
      typeFormat(row) {
        return this.selectDictLabel(this.typeOptions, row.type)
      },
      // 方向：流入、流出字典翻译
      directionFormat(row) {
        return this.selectDictLabel(this.directionOptions, row.direction)
      },
      // 业务类型：基金缴款 基金分配字典翻译
      businessTypeFormat(row) {
        return this.selectDictLabel(this.businessTypeOptions, row.businessType)
      },
      // 是否参与IRR计算字典翻译
      whetherIrrFormat(row) {
        return this.selectDictLabel(this.whetherIrrOptions, row.whetherIrr)
      },

      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          lpId: undefined,
          fundId: undefined,
          type: undefined,
          operateDate: undefined,
          operateAmount: undefined,
          direction: '0',
          businessType: undefined,
          whetherIrr: '0',
          remark: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          delFlag: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.dateRange = []
        this.queryHandle()
      },
      /** 新增按钮操作 */
      addHandle() {
        this.formOpen = true
        this.title = this.$t('出资人现金流')
        this.$nextTick(() => {
          this.$refs.LpCashflowForm.init(null)
        })
      },
      /** 修改按钮操作 */
      updateHandle(row) {
        this.formOpen = true
        this.title = this.$t('出资人现金流')
        this.$nextTick(() => {
          // 调用form页面的 init 方法初始化数据
          this.$refs.LpCashflowForm.init(row)
        })
      },
      /** 删除按钮操作 */
      deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        }).then(function() {
          return delLpCashflow(row.id)
        }).then(() => {
          this.getList()
          this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
      },
      /** 导出按钮操作 */
      exportHandle() {
        this.downLoadExcel('/cashflow/lpCashflow/export', this.addDateRange(this.queryParams, this.operateDates))
      },
      getIrr() {
        getLpCashflowIrr(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
          let msg = ''
          if (res.data === 502) {
            msg = this.$t('计算失败：小于或等于一条现金流')
          } else if (res.data === 503) {
            msg = this.$t('计算失败：第一条现金流的时间不是最早时间')
          } else if (res.data === 504) {
            msg = this.$t('计算失败：第一条现金流的payment的值不为负')
          } else {
            msg = res.data
          }
          this.$alert(msg, '当前数据IRR', {
            dangerouslyUseHTMLString: true
          })
        })
      },
      // 初始化币种信息
      async initCurr(val) {
        let param = this.$route.query.id
        if (this.$route.query.id === undefined) {
          param = val
        }
        await getFundBase(param).then(res => {
          this.fundCurr = res.data.currency
        })
        await getSystemCurrency().then(res => {
          if (res.code === 200) {
            this.defCurr = res.data
          } else {
            this.msgError(res.msg)
          }
        })
        this.isRate = this.defCurr !== this.fundCurr
      },
      // 查询irr、dpi和moic
      getCashFlowCombined() {
        if (this.queryParams.fundId !== undefined) {
          getCashFlowCombined(this.queryParams.fundId).then(response => {
            this.cashflowVO = response.data
          })
        }
      }
    }
  }
</script>
